<?php
namespace app\api\controller;
use think\Exception;
use think\Request;
use org\Jdk_inc;
use app\api\controller\Base;

/**
 * 公共信息
 */
class User extends Base
{
    /**
     * 获取用户信息
     * @url     
     * @http    post
     * @data    
     * @return  array of getGoodsTypeList item , code 0
     *
     */
    public function getUserInfo()
    {
        if (request()->isPost()) {
            $user_id =  $this->user_id;
            $userinfo  = db('user')->where(['user_id'=>$user_id])->find();
            $jdk = new Jdk_inc();
            if($userinfo['eqimg']=="" || !isset($userinfo['eqimg'])){
                $eqimg = $jdk->getWxCode($user_id);
                db('user')->where(['user_id'=>$user_id])->update(['eqimg'=>$eqimg]);
            }
            vipnumber($user_id);
            $list = db('user')->where(['user_id'=>$user_id])->find();
            $list['vip'] = db('vip_cate')->where(['level'=>$list['vip_id']])->find();
            $time = time();
            $list['coupon'] = db('coupon_user')->where(['user_id'=>$user_id,'status'=>0])->select();
            $list['coupon'] = count($list['coupon']);
            return model('Res')->ctx($list);
        } else {
            return model('Res')->noPost();
        }
    }
    
    //判断手机号是否存在
    public function checkmobile(){
        $user_id = $this->user_id;
        $user = db('user')->where(['user_id'=>$user_id])->field('phone')->find();
        if(!$user['phone']){
            return json(['code'=>0,'msg'=>"您还未绑定手机号"]);
        }else{
            return json(['code'=>1,'msg'=>"已经绑定了"]);
        }
    }
    // 判断用户支付密码是否存在
    public function checkpaypwd(){
        $user_id = $this->user_id;
        $user = db('user')->where(['user_id'=>$user_id])->field('paypwd')->find();
        return model('Res')->ctx($user);
    }
    // 修改用户信息
    public function updateUser()
    {
        if(request()->isPost()){
            $realname = input('post.realname');
            $phone = input('post.phone');
            $sex = input('post.sex');
            $idcode = input('post.idcode');
            if(!isset($realname)&&!isset($phone)&&!isset($sex)&&!isset($idcode)){
                return model('Req')->noParam();
            }
            $data = input('post.');
            $user_id =  $this->user_id;
            $list = db('user')->where(['user_id'=>$user_id])->update($data);
            return ['code'=>1,msg=>'修改成功'];
        }else{
            return model('Req')->noPost();
        }
        
    }
    /***********上传图片***********/
    public function upload()
    {
        $param = input('param.');
        
       
        $up_dir = ROOT_PATH . 'public' . DS . 'uploads/' . date('Ymd') . '/'; //存放在当前目录的upload文件夹下
        if (!file_exists($up_dir)) {
            mkdir($up_dir, 0777);
            chmod($up_dir, 0777);
        }
        $base64_img = trim($param['file']);
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)) {
            $type = $result[2];
            if (in_array($type, array('pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png'))) {
                $fileName = time().rand(100,999).'.'.$type;
                $new_file = $up_dir . $fileName;
                if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))) {
                    $img_path = str_replace('../../..', '', $new_file);
                    
                    
                    
                    $dst_img = ROOT_PATH . 'public' . DS . 'uploads/' . date('Ymd') . '/' . $fileName; //存放路径
                    
                    return json(['code'=>1,'msg'=>"上传成功",'data'=>"http://" . $_SERVER['HTTP_HOST'] . '/uploads/' . date('Ymd') . '/' . $fileName]);
                     
                } else {
                     
                    return ['code'=>0,'msg'=>'图片上传失败'];
                }
            } else {
                
                return ['code'=>0,'msg'=>'图片上传失败'];
            }
        }
        return ['code'=>0,'msg'=>'图片上传失败'];
        
    }
    
    //取消订单
    public function cancelorder(){
        
        $order_id = input('order_id');
        if(!$order_id) return json(['code'=>0,'msg'=>"参数错误"]);
        $order = db('order')->where(['order_id'=>$order_id])->find();
        if(empty($order)) return json(['code'=>0,'msg'=>"订单不存在"]);
        
        if($order['status']!=0 && $order['status']!=7 && $order['status']!=3 && $order['status']!=6){
            return json(['code'=>0,'msg'=>"订单状态错误"]);
        }
        db('order')->where(['order_id'=>$order_id])->update(['is_delete'=>1]);
        return json(['code'=>1,'msg'=>"订单取消成功"]);
    }
    
    
    //申请退款
    public function refundorder(){
        
        $order_id = input('order_id');
        $refund_type = input('refund_type');
        $refund_note = input('refund_note');
        if(!$order_id) return json(['code'=>0,'msg'=>"参数错误"]);
        $order = db('order')->where(['order_id'=>$order_id])->find();
        if(empty($order)) return json(['code'=>0,'msg'=>"订单不存在"]);
        
        if($order['status']!=1){
            return json(['code'=>0,'msg'=>"订单状态错误"]);
        }
        db('order')->where(['order_id'=>$order_id])->update(['status'=>2,'refund_type'=>$refund_type,'refund_note'=>$refund_note]);
        return json(['code'=>1,'msg'=>"申请退款成功，等待后台审核"]);
    }
    
    //查看退款审核结果
    public function refundreason(){
        
        $order_id = input('order_id');
        if(!$order_id) return json(['code'=>0,'msg'=>"参数错误"]);
        $order = db('order')->where(['order_id'=>$order_id])->find();
        if(empty($order)) return json(['code'=>0,'msg'=>"订单不存在"]);
        
        $data = db('order')->where(['order_id'=>$order_id])->value('check_reason');
        return json(['code'=>1,'msg'=>"查询成功",'data'=>$data]);
        
    }
    
    //充值卡列表
    public function rechargeCard(){
        
        $id = input('id');

        if(isset($id) && $id){
            $activity = db('activity')->where(['id'=>$id])->find();
            $list = db('recharge_card')->where(['status'=>0,'number'=>['>',0],'cate'=>0,'id'=>['in',$activity['param']],'endtime'=>['gt',time()]])->field('id,title,money,img,back_money,endtime')->select();
        }else{
            $list = db('recharge_card')->where(['status'=>0,'number'=>['>',0],'cate'=>0,'is_activity'=>0,'endtime'=>['gt',time()]])->field('id,title,money,img,back_money,endtime')->select();
        }
        $list = $list?:[];
        foreach ($list as $i => $item) {
            $list[$i]['endtime'] = date('Y-m-d H:i:s',$item['endtime']);
        }
        return json(['code'=>1,'msg'=>"查询成功",'data'=>$list]);
    }
    
    //次卡列表
    public function secondCard(){
        $id = input('id');
        if(isset($id) && $id){
            $activity = db('activity')->where(['id'=>$id])->find();
            $list = db('second_card')->where(['status'=>0,'cate'=>0,'number'=>['>','0'],'id'=>['in',$activity['param']]])->field('id,title,num,addtime,money,title,endtime,logo')->order('id desc')->select(); 
        }else{
            $list = db('second_card')->where(['status'=>0,'cate'=>0,'number'=>['>','0'],'is_activity'=>0])->field('id,title,num,addtime,money,title,endtime,logo')->order('id desc')->select();  
        }
        
        $list = $list?:[];
        foreach ($list as $i => $item) {
            if($item['endtime']==0){
                $list[$i]['endtime'] = '无期限';
            }else{
                $list[$i]['endtime'] = $item['endtime']*24*60*60+$item['addtime']*1;
                $list[$i]['endtime'] = date('Y-m-d',$list[$i]['endtime']);
            }
        }

        return json(['code'=>1,'msg'=>"查询成功",'data'=>$list]);
    }
    
}
